package cn.xuanxuanyuhua.crm.service.impl;


import cn.xuanxuanyuhua.crm.domain.Customer;
import cn.xuanxuanyuhua.crm.domain.CustomerTransfer;
import cn.xuanxuanyuhua.crm.domain.Employee;
import cn.xuanxuanyuhua.crm.mapper.CustomerMapper;
import cn.xuanxuanyuhua.crm.query.QueryObject;
import cn.xuanxuanyuhua.crm.service.ICustomerService;
import cn.xuanxuanyuhua.crm.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CustomerServiceImpl implements ICustomerService {
    @Autowired
    private CustomerMapper customerMapper;


    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Customer> list = customerMapper.selectAll(qo);
        return new PageInfo(list);
    }

    @Override
    public void updateInfo(CustomerTransfer entity) {
        customerMapper.updateInfo(entity.getCustomer().getId(),entity.getNewSeller().getId(),Customer.NORMAL);
    }

    @Override
    public void updateStatus(Customer entity) {

        try {
            customerMapper.updateInfo(entity.getId(),null,entity.getStatus());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void saveOrUpdate(Customer entity) {
        if (entity.getId() != null) {
            customerMapper.updateByPrimaryKey(entity);
        }else{
            Employee employee = UserContext.getActiveUser();
            entity.setInputUser(employee);
            entity.setSeller(employee);
            entity.setInputTime(new Date());
            entity.setStatus(Customer.NORMAL);
            customerMapper.insert(entity);
        }
    }



}
